package com.inxedu.os.edu.controller.course;

import com.google.gson.Gson;
import com.inxedu.os.common.controller.BaseController;
import com.inxedu.os.edu.pojo.SysSubject;
import com.inxedu.os.edu.service.course.CourseSubjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 课程 专业
 */
@Controller
@RequestMapping("/admin")
@Api(value = "专业列表", tags = "专业列表")
public class CourseSubjectController extends BaseController {
    @InitBinder({"subject"})
    public void initBinderSysFunction(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("subject.");
    }
    @Autowired
    private CourseSubjectService courseSubjectServiceimpl;

    @RequestMapping(value = "/subj/toSubjectList",method = RequestMethod.GET)
    //@ApiImplicitParam(name = "courseId", value = "课程的Id", required = true, dataType = "courseId")
    @ApiOperation(value = "查询专业", notes = "查询专业")
    public String showCourseSubject(HttpServletRequest request){

        Gson gson=new Gson();
        List<SysSubject> list=courseSubjectServiceimpl.findCourseSubjectAll();
       request.setAttribute("subjectList",gson.toJson(list));

        return "/view/inxedu/admin/subject/subject_list";
    }

    /**
     * 修改专业名字
     * @param subjectName
     * @param subjectId
     * @return
     */
    @RequestMapping(value = "/subj/updatesubjectName",method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "subjectName", value = "课程名", required = true, dataType = "subjectName"),
            @ApiImplicitParam(name = "subjectId", value = "专业Id", required = true, dataType = "subjectId")
    })
    @ApiOperation(value = "修改专业名字", notes = "修改专业名字")
    @ResponseBody
    public Map<String,Object> updateSubjectNameByid(@RequestParam String subjectName,@RequestParam Integer subjectId){
        Map<String, Object> map = new HashMap<String, Object>();
        int i=courseSubjectServiceimpl.updateSubjectNameByid(subjectName,subjectId);
        if(i>0){
            map=this.setJson(true,"保存成功！",null);

        }else {
            map=this.setJson(false,"保存失败！",null);
        }
        return map;
    }

    /**
     * 删除专业
     * @param subjectId
     * @return
     */
    @RequestMapping(value = "/subj/deleteSubject/{subjectId}",method = RequestMethod.POST)
    @ApiImplicitParam(name = "subjectId", value = "专业Id", required = true, dataType = "subjectId")
    @ApiOperation(value = "删除专业", notes = "删除专业")
    @ResponseBody
    public Map<String,Object> deleteSubjectNameByid( @PathVariable Integer subjectId) {
        Map<String, Object> map = new HashMap<String, Object>();
        int i=courseSubjectServiceimpl.deleteSubjectNameByid(subjectId);
        if(i>0){
            map=this.setJson(true,"保存成功！",null);

        }else {
            map=this.setJson(false,"保存失败！",null);
        }
        return map;
    }

    /**
     * 添加专业
     * @param subject
     * @return
     */
    @RequestMapping(value = "/subj/createSubject",method =RequestMethod.POST)
    @ApiImplicitParam(name = "SysSubject", value = "专业对象", required = true, dataType = "SysSubject")
    @ApiOperation(value = "添加专业", notes = "添加专业")
    @ResponseBody
    public Map<String,Object>  createSubject(@ModelAttribute("subject") SysSubject subject) {
        Map<String, Object> map = new HashMap<String, Object>();
        int i=courseSubjectServiceimpl.addSubject(subject);
        if(i>0){
            map=this.setJson(true,"新增成功！",subject);

        }else {
            map=this.setJson(false,"新增失败！",subject);
        }
        return map;
    }

}
